﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Familia
''' </summary>
''' <remarks></remarks>
Public Class DALFamilia

    Private Shared m_Instance As DALFamilia
    Private Shared m_Semaforo As New Object
    Private m_TDGFamilia As TDGFamilia


    Public Function Insert(obj As cFamilia) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGFamilia.Insert(database.Conexion, obj.Fam_Descrip, obj.DVH, obj.Fam_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloAlta(ex)
        End Try

    End Function

    Public Function Update(obj As cFamilia) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            m_TDGFamilia.Update(database.Conexion, obj.Fam_Codigo, obj.Fam_Descrip, obj.DVH)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloEditar(ex)
        End Try
    End Function

    Public Function Delete(obj As cFamilia) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            m_TDGFamilia.Delete(database.Conexion, obj.Fam_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloBaja(ex)
        End Try

    End Function

    Public Function Sel(obj As cFamilia) As cFamilia
        Dim dr As SqlDataReader
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGFamilia.Find(database.Conexion, obj.Fam_Codigo)

            If dr.HasRows Then

                dr.Read()

                obj.Fam_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                obj.Fam_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                obj.DVH = Utils.Convert.ToInt32(dr.GetValue(2))

                dr.Close()
            Else
                obj = Nothing
            End If

            database.Desconectar()

            Return obj
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloBuscar(ex)
        End Try
    End Function

    Public Function SelectAll() As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cFamilia
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGFamilia.FindAll(database.Conexion)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cFamilia

                    objAux.Fam_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.Fam_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                    objAux.DVH = Utils.Convert.ToInt32(dr.GetValue(2))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloConsultar(ex)
        End Try
    End Function

    Public Function Select_Patentes(Fam_Codigo As Int16) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cPatente
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGFamilia.Find_Patentes(database.Conexion, Fam_Codigo)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cPatente

                    objAux.Pat_Codigo = Utils.Convert.ToInt16(dr.GetInt16(0))
                    objAux.Pat_Descripcion = Utils.Convert.ToStr(dr.GetString(1))
                    objAux.Checked = Utils.Convert.ToBool(dr.GetValue(2))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloConsultar(ex)
        End Try

    End Function

    Public Function Insert_Patente(objFamilia As cFamilia, objPatente As cPatente) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGFamilia.Insert_Patente(database.Conexion, objFamilia.Fam_Codigo, objPatente.Pat_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloEditar(ex)
        End Try

    End Function

    Public Function Delete_Patente(objFamilia As cFamilia, objPatente As cPatente) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGFamilia.Delete_Patente(database.Conexion, objFamilia.Fam_Codigo, objPatente.Pat_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.FamiliaFalloEditar(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALFamilia
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New DALFamilia
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGFamilia = TDGFamilia.Instance()
    End Sub

End Class
